import { createRouter, createWebHistory } from 'vue-router';
import { getToken } from '@/utils/token';

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/',
      meta: {
        title: '用户列表',
      },
      component: () => import('../views/user/index.vue'),
    },
  ],
});

router.beforeEach((to, from, next) => {
  if (to.matched.some((item) => item.meta.auth)) {
    const token = getToken();
    if (token) {
      return next();
    } else {
      return next({
        name: 'login',
        query: {
          redirect: to.fullPath,
        },
      });
    }
  } else {
    return next();
  }
});

export default router;
